import { message } from 'antd';
import {
  queryXcxList,
  queryXcxTmpList,
  setXcxSchedule,
  queryTask,
  deleteTask,
} from '../services/wx';

export default {
  namespace: 'xcx_template_message',

  state: {
    xcxList: [],
    tmpList: false,
    schedules: [],
    account: null,
  },

  effects: {
    // 查询公众号列表
    *fetchXcxList(_, { call, put }) {
      const res = yield call(queryXcxList);
      yield put({
        type: 'saveXcxList',
        payload: res,
      });
    },
    // 查询公众号下的可配置的模板消息列表
    *fetchXcxTmpList({ payload }, { call, put }) {
      const res = yield call(queryXcxTmpList, payload);
      yield put({
        type: 'saveTmpList',
        payload: res,
      });
    },
    // 创建发送任务
    *createSchedule({ payload }, { call }) {
      const res = yield call(setXcxSchedule, payload);
      if (res === 'ok') {
        message.success('操作成功');
      }
    },
  },

  reducers: {
    // 保存账号列表
    saveXcxList(state, { payload }) {
      return {
        ...state,
        xcxList: [payload],
      };
    },
    // 保存模板列表
    saveTmpList(state, { payload }) {
      return {
        ...state,
        tmpList: { ...payload },
      };
    },
  },
};
